/*
 * @Date: 2023-11-02 16:28:02
 * @LastEditors: lyx
 * @LastEditTime: 2024-01-31 10:29:51
 * @FilePath: \mt-middleware-server\config\config.ts
 * @Description:
 */
import { readFile } from 'fs/promises';
import * as yaml from 'js-yaml';
import { join } from 'path';
import { NacosManager } from './nacos';

const setupNacosConfig = async (): Promise<any> => {
  const configManager = new NacosManager();
  const nacosConfigs: any = await configManager.getAllConfig();
  console.log(nacosConfigs, '配置内容');
  return nacosConfigs;
};

export default async () => {
  const isDev = process.env.NODE_ENVIRONMENT === 'production' ? false : true;
  const fileName = isDev ? 'dev' : 'pro';
  console.log('fileName', fileName);
  const configFilePath = join(process.cwd(), `.${fileName}.yaml`);
  const nacosRes = await setupNacosConfig();
  console.log('nacosRes', yaml.load(nacosRes));
  const config = await readFile(configFilePath, {
    encoding: 'utf-8',
  });

  return yaml.load(config,nacosRes);
};
